package x2rel.qMap.core;

import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlHandler extends Utils{

    //Limpar espaços em branco e comentários do xml
    protected void clearDocument(Node document) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource("files/strip-space.xsl"));
            DOMSource source = new DOMSource(document);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            transformer.transform(source, result);
            //Writer writer = result.getWriter()
            while (true) {
                //if (writer == null) {
                Thread.sleep(3000);
                //} else {
//                    StringWriter sw = (StringWriter) result.getWriter();
//                    StringBuffer sb = sw.getBuffer();
//                    String finalstring = sb.toString();
//                    System.out.println("Há! - " + finalstring);
                System.out.println("Há! - " + writer.toString());
                //}

            }


        } catch (InterruptedException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected Node clearDocument2(Node document) {


        try {

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = factory.newTransformer(
                    new StreamSource("files/strip-space.xsl"));

            DOMSource source = new DOMSource(document);

//            StringWriter writer = new StringWriter();
            //StreamResult result = new StreamResult(writer);
            StreamResult result = new StreamResult(System.out);

            //transformer.transform(source, new StreamResult(writer));
            transformer.transform(source, result);
            System.exit(0);
//            String strnode = writer.toString(); 
            //String strnode = result.getWriter().toString();
            //System.out.println("Teste: " +strnode);
//            Logging.getInstance().log("TESTE:" + strnode,3);
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            InputSource is = new InputSource();

            //is.setCharacterStream(new StringReader(strnode));
            Document doc;
            doc = db.parse(is);
            //return doc.getDocumentElement().removeChild();//FirstChild().removeChildren();
            return (Node) doc;

            //System.exit(1);
            //return result;


        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);

        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    protected Boolean isElementNode(Node n) {
        return (n.getNodeType() == Node.ELEMENT_NODE);
    }

    protected Integer countChildrenElements(NodeList nl) {
        Integer count = 0;
        if (nl != null) {
            if (nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {
                    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                        count += 1;
                    }
                }
            }
        }
        return count;
    }

    protected Boolean hasChildren(NodeList nl) {
        if (nl != null) {
            if (nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {
                    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
